package com.songyang.mqttandothers.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.songyang.mqttandothers.bean.DeviceBean;
import com.songyang.mqttandothers.bean.LogBean;
import com.songyang.mqttandothers.common.ResultMapUtil;
import com.songyang.mqttandothers.service.DeviceService;
import com.songyang.mqttandothers.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/device")
public class deviceController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private LogService logService;
    private String redisListKey = "mqttList";
    private String graphKey = "graphMapKey";


    @GetMapping("/bind/{userId}/{deviceId}/{deviceName}")
    public Object bind(@PathVariable Integer userId, @PathVariable Integer deviceId, @PathVariable String deviceName) {
        Integer bind = deviceService.bind(userId, deviceId, deviceName);
        if (bind != null) return ResultMapUtil.bind(bind);
        else return ResultMapUtil.fail("失败");
    }

    @GetMapping("/bind/{deviceId}")
    public Object bind(@PathVariable Integer deviceId) {
        return ResultMapUtil.getHashMapClientId(deviceId);
    }

    @GetMapping("/unBind/{userId}/{deviceId}")
    public Object unBind(@PathVariable Integer userId, @PathVariable Integer deviceId) {
        boolean bind = deviceService.unBind(userId, deviceId);
        if (bind) return ResultMapUtil.success();
        else return ResultMapUtil.fail("失败");
    }


    @GetMapping("/setMessage/{userId}/{deviceId}/{msg}")
    public Object sendMessage(@PathVariable Integer userId, @PathVariable Integer deviceId, @PathVariable String msg) {
        String value = userId + ":" + deviceId + ":" + msg;
        redisTemplate.opsForList().leftPush(redisListKey, value);
        return ResultMapUtil.success();
    }

    @GetMapping("/getAllDevices/{userId}")
    public Object getAllDevices(@PathVariable Integer userId) {
        List<DeviceBean> allList = deviceService.getAllList(userId);
        if (allList != null && allList.size() > 0)
            return ResultMapUtil.getHashMapMysqlPage(allList);
        else return ResultMapUtil.fail("失败");
    }

    @GetMapping("/getAllLog/{userId}/{nuPage}/{pageSize}/{keyWord}")
    public Object getAllLog(@PathVariable Integer userId, @PathVariable String keyWord, @PathVariable Integer nuPage, @PathVariable Integer pageSize) {
        IPage<LogBean> allLog = logService.getAllLog(userId, keyWord, nuPage, pageSize);
        if (allLog != null && allLog.getSize() > 0) return ResultMapUtil.getHashMapMysqlPage(allLog);
        else return ResultMapUtil.fail("失败");
    }

    @GetMapping("/getGraph/{userId}/{deviceId}")
    public Object getGraph(@PathVariable Integer userId, @PathVariable Integer deviceId) {
        String topic = deviceService.getChannel(userId, deviceId);
        if (topic == null) return ResultMapUtil.fail("没有此设备！");
        String hashKey = graphKey + ":" + topic;
        Set keys = redisTemplate.opsForHash().keys(hashKey);
        Iterator iterator = keys.iterator();
        ArrayList<String> time = new ArrayList<>();
        ArrayList<String> value = new ArrayList<>();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            time.add(key);
            String hashValue = (String) redisTemplate.opsForHash().get(hashKey, key);
            value.add(hashValue);
        }
        return ResultMapUtil.getGraphHashMap(time, value);
    }

    @GetMapping("/find/{deviceId}/{nuPage}/{pageSize}")
    public Object find(@PathVariable Integer deviceId, @PathVariable Integer nuPage, @PathVariable Integer pageSize) {
        IPage<LogBean> allLog = logService.find(deviceId, nuPage, pageSize);
        if (allLog != null && allLog.getSize() > 0) return ResultMapUtil.getHashMapMysqlPage(allLog);
        else return ResultMapUtil.fail("失败");
    }
}
